package com.example.service;


import com.example.entity.Article;
import com.example.repository.ArticleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class ApiArticleService {
    @Autowired
    ArticleRepository articleRepository;

    @Autowired
    RedisTemplate redisTemplate;

    public Article findArticleById(Integer id) {
        Object object = redisTemplate.opsForValue().get("article_api" + id);
        if (object != null) {
            return (Article) object;
        } else {
            Optional<Article> article = articleRepository.findById(id);
            if (article.isPresent()) {
                redisTemplate.opsForValue().set("article_api" + id, article.get());
            }
            return article.get();
        }
    }
}
